package com.party.course.service.impl;

import java.util.List;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.party.course.mapper.WageInfoMapper;
import com.party.course.domain.WageInfo;
import com.party.course.service.IWageInfoService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.party.common.utils.SecurityUtils;

/**
 * 工资管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class WageInfoServiceImpl extends ServiceImpl<WageInfoMapper, WageInfo> implements IWageInfoService {
    @Autowired
    private WageInfoMapper wageInfoMapper;

    /**
     * 查询工资管理
     *
     * @param id 工资管理主键
     * @return 工资管理
     */
    @Override
    public WageInfo view(String id) {
        return wageInfoMapper.selectWageInfoById(id);
    }

    /**
     * 查询工资管理列表
     *
     * @param wageInfo 工资管理
     * @return 工资管理
     */
    @Override
    public List<WageInfo> findList(WageInfo wageInfo) {
        return wageInfoMapper.selectWageInfoList(wageInfo);
    }
    /**
     * 新增工资管理
     *
     * @param wageInfo 工资管理
     * @return 结果
     */
    @Override
    public int add(WageInfo wageInfo) {
        //工资绩效下发后生效时间暂定为1天后
        DateTime date = DateUtil.date();
        String newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1).toString();
        //目前只能设置默认工资标准 也就是每种类型只能有一条数据
        WageInfo wage = wageInfoMapper.findByType(wageInfo.getType());
        if (wage != null) {
            wage.setBaseWage(wageInfo.getBaseWage());
            wage.setPerWage(wageInfo.getPerWage());
            wage.setTotalWage(wageInfo.getTotalWage());
            wage.setSubsidy(wageInfo.getSubsidy());
            wage.setAward(wageInfo.getAward());
            wage.setCreateTime(date);
            wage.setValidTime(newDate);
            return updateEntity(wage);
        }else {
            wageInfo.setId(IdUtil.simpleUUID());
            wageInfo.setDelFlag("0");
            wageInfo.setCreateTime(date);
            wageInfo.setValidTime(newDate);
            return wageInfoMapper.insert(wageInfo);
        }
    }

    /**
     * 修改工资管理
     *
     * @param wageInfo 工资管理
     * @return 结果
     */
    @Override
    public int updateEntity(WageInfo wageInfo) {
        wageInfo.setUpdateTime(DateUtil.date());
        return wageInfoMapper.updateById(wageInfo);
    }

    /**
     * 批量删除工资管理
     *
     * @param ids 需要删除的工资管理主键
     * @return 结果
     */
    @Override
    public int deleteIds(List<String> ids) {
        return wageInfoMapper.deleteWageInfoByIds(ids);
    }

    /**
     * 删除工资管理信息
     *
     * @param id 工资管理主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return wageInfoMapper.deleteWageInfoById(id);
    }
}
